using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace InfiniTec.Data
{
	public class PropertyDefinition<T> : IEquatable<PropertyDefinition<T>>, IPropertyDefinition
	{
		private readonly ReadOnlyCollection<IPropertyDefinition> _DependentProperties;
		private readonly bool _IsMultivalued;
		private readonly string _Name;

		public PropertyDefinition(string name, bool isMultivalued, params IPropertyDefinition[] dependentProperties)
		{
			_Name = name;
			_IsMultivalued = isMultivalued;
			_DependentProperties = new List<IPropertyDefinition>(dependentProperties ?? new IPropertyDefinition[0]).AsReadOnly();
		}

		public bool Equals(PropertyDefinition<T> propertyDefinition)
		{
			return Equals((IPropertyDefinition) propertyDefinition);
		}


		public ReadOnlyCollection<IPropertyDefinition> DependentProperties
		{
			get { return _DependentProperties; }
		}

		public bool IsMultivalued
		{
			get { return _IsMultivalued; }
		}

		Type IPropertyDefinition.Type
		{
			get { return typeof (T); }
		}

		public string Name
		{
			get { return _Name; }
		}

		public bool Equals(IPropertyDefinition propertyDefinition)
		{
			if (propertyDefinition == null) return false;
			return Equals(_Name, propertyDefinition.Name);
		}


		public override bool Equals(object obj)
		{
			if (ReferenceEquals(this, obj)) return true;
			return Equals(obj as PropertyDefinition<T>);
		}

		public override int GetHashCode()
		{
			return _Name.GetHashCode();
		}
	}
}